package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.modular.ctm.service.UploadService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/api/file")
@Api(tags = "文件管理")
@CrossOrigin
public class FileUploadController {

    @Autowired
    private UploadService uploadService;

    @RequestMapping("/uploadAvatar")
    @ResponseBody
    public ResponseData uploadAvatar(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ResponseData.error("上传文件不能为空");
        }
        return ResponseData.success(uploadService.uploadAvatar(file));
    }

    @RequestMapping("/uploadResumeFile")
    @ResponseBody
    public ResponseData uploadResumeFile(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ResponseData.error("上传文件不能为空");
        }
        return ResponseData.success(uploadService.uploadResumeFile(file));
    }

    @GetMapping("/image")
    public ResponseEntity<Resource> getImage(String path) {
        // 加载图片资源
        Resource resource = uploadService.loadResource(path);
        // 构建响应
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_JPEG)
                .body(resource);
    }

}
